import logging
from offline_calc.handler import OfflineCalculationHandler
from django.conf import settings
from event_rule.cp.op_event_rule import rule_config
from common.cp.maestro import op_analysis_handler
from common.const import IntervalType, INTERVAL_TYPE_MAPPER

_LOGGER = logging.getLogger(__name__)


class Command(OfflineCalculationHandler):
    def __init__(self, *args, **kwargs):
        super(Command, self).__init__(*args, **kwargs)
        self.index_list = settings.CP_INDEX
        self.event_config = rule_config.CONFIG

    def add_arguments(self, parser):
        super(Command, self).add_arguments(parser)

    def handle(self, **kwargs):
        super(Command, self).handle(**kwargs)
        merchant = kwargs['merchant']
        target_event = kwargs['target_event']
        self.index_list = self.index_list if not merchant else [index for index in self.index_list if index == merchant]
        self.event_config = self.event_config if not target_event else {
            event_name: rule for event_name, rule in self.event_config.items() if event_name == target_event}
        self.event_config = {event_name: rule for event_name, rule in self.event_config.items()
                             if not rule.get('real_time_report', False)}
        for index in self.index_list:
            window_visited = {}
            for op_event_name, config in self.event_config.items():
                interval_type_list = config.get('interval_type_list', [])
                group_by = config.get('group_by')
                for interval_type_value in interval_type_list:
                    window_list = self.get_window_interval(interval_type_value, config.get('day_to_calc', [0]))
                    for window in window_list:
                        interval_type = INTERVAL_TYPE_MAPPER[interval_type_value]
                        if not self.check_merchant_start_time(interval_type, index, window):
                            continue
                        if not isinstance(interval_type, IntervalType):
                            continue
                        print(op_event_name, interval_type_value, window)
                        if not group_by:
                            self.pool.apipe(self.start_execution, op_analysis_handler.op_report_handler, interval_type,
                                            op_event_name, self.base_time, index, window,
                                            dict(interval_type=interval_type, base_time=self.base_time,
                                                 last_window=window,
                                                 event_name=op_event_name, event_rule=config, index=index))
                        else:
                            disable_global_aggregate = config.get('disable_global_aggregate', False)
                            self.pool.apipe(self.start_execution, op_analysis_handler.op_group_by_report_handler,
                                            interval_type, op_event_name, self.base_time, index, window,
                                            dict(interval_type=interval_type, base_time=self.base_time,
                                                 last_window=window,
                                                 event_name=op_event_name, event_rule=config, index=index,
                                                 disable_global_aggregate=disable_global_aggregate))
                        window_visited.setdefault(op_event_name, {})
                        window_visited[op_event_name].setdefault(interval_type_value, [])
                        window_visited[op_event_name][interval_type_value].append(window)

        self.pool.close()
        self.pool.join()
